package com.tjetc.Impl;

import com.tjetc.domain.*;
import com.tjetc.mapper.HealthMapper;
import com.tjetc.mapper.NewbornMapper;
import com.tjetc.mapper.ParentMapper;
import com.tjetc.mapper.VaccinationMapper;
import com.tjetc.service.ParentService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

import java.util.ArrayList;
import java.util.List;

public class ParentServiceImpl implements ParentService {
    private Result result=new Result();
    private ParentMapper parentMapper= Db.getMapper(ParentMapper.class);
    private NewbornMapper newbornMapper=Db.getMapper(NewbornMapper.class);
    private HealthMapper healthMapper=Db.getMapper(HealthMapper.class);
    private VaccinationMapper vaccinationMapper=Db.getMapper(VaccinationMapper.class);
    @Override
    public Result add(Parent parent) {
        String username = parent.getUsername();
        Parent parentByName = parentMapper.getParentByName(username);
        if (parentByName!=null){
            return result.setMsg("用户名已存在").setCode(500);
        }
        int id=parentMapper.insert(parent);
        if (id==1){
            return result.setMsg("添加成功").setCode(200);
        }return result.setMsg("添加失败").setCode(500);
    }

    @Override
    public Result update(Parent parent) {//修改
        String username = parent.getUsername();
        Parent parentByName = parentMapper.getParentByName(username);
        Parent partentById = parentMapper.selectByPrimaryKey(parent.getId());
        if (partentById==null){
            System.out.println("没有这个用户");
            return result.setCode(500).setMsg("没有这个用户");
        }
        if (parentByName==null){
            int i = parentMapper.updateByPrimaryKey(partentById);
            if (i==1){
                return result.setMsg("修改成功").setCode(200);
            }else return result.setMsg("修改失败").setCode(500);
        }else{
            return result.setMsg("用户名已存在").setCode(500);
        }
    }

    @Override
    public Result delete(int id) {
        int i = parentMapper.deleteByPrimaryKey(id);
        if (i==1){
            return result.setMsg("删除成功").setCode(200);
        }
        return result.setMsg("删除失败").setCode(500);
    }

    @Override
    public Parent getParentById(int id) {
        return parentMapper.selectByPrimaryKey(id);
    }

    @Override
    public Result add1(Parent parent) {
        try {
            Parent parent1=parentMapper.findByNameAPId(parent.getUsername(),parent.getId());
//            Newborn newborn1=newbornMapper.findByNameAPId(newborn.getName(),newborn.getParentId());
            if (parent1!=null){
                return result.setCode(500).setMsg("该家长已经存在");
            }else {
                int rows=parentMapper.insert(parent);
                return rows==1?result.setCode(200).setMsg("添加成功"):result.setCode(500).setMsg("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.setCode(500).setMsg("添加失败");
    }

    @Override
    public Result findByNameAP(Parent parent) {
        Parent parent1=parentMapper.findByName(parent.getUsername(),parent.getPassword());
//        Doctor doctor1=doctorMapper.findByName(doctor.getUsername());
        while (true){
            if (parent1==null){
                return result.setCode(500).setMsg("输入错误");
            }
            break;
        }
        return result.setCode(200).setMsg("登录成功").setData(parent1);
    }



    @Override
    public List<Newborn> sel2(int parentId) {
        return newbornMapper.list1(parentId);
    }

    @Override
    public List<Health> serch1(int parentId) {
        List<Newborn> list = newbornMapper.selBP(parentId);
        List<Health> resultList = new ArrayList<>();
        for (Newborn newborn : list) {
            if (newborn != null) {
                int newbornId = newborn.getId();
                List<Health> healthList = healthMapper.getDiseasesWithHealthInfo1(newbornId);
                resultList.addAll(healthList);
            }
        }
        return resultList;
    }

    @Override
    public List<Vaccination> selY(int parentId) {
        List<Newborn> list = newbornMapper.selBP(parentId);
        List<Vaccination> resultList = new ArrayList<>();
        for (Newborn newborn : list) {
            if (newborn!=null){
                int newbornId = newborn.getId();
                List<Vaccination> vaccinationList=vaccinationMapper.get(newbornId);
                resultList.addAll(vaccinationList);
            }
        }
        return resultList;
    }

    @Override
    public Result upd(String password2, int parentId) {
        try {
            int rows=parentMapper.updPa(password2,parentId);
            if(rows==-1){
                return result.setCode(500).setMsg("修改失败");
            }else {
                return result.setCode(200).setMsg("更新成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.setCode(500).setMsg("修改失败");
    }

    @Override
    public Parent findBI(int id) {
        return parentMapper.selectByPrimaryKey(id);
    }
}
